//
//  SaveTool.m
//  豆瓣
//
//  Created by 韩震明 on 15/10/14.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SaveTool.h"

@implementation SaveTool

+ (void)isHaveFolder
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *collectionPath = [sandBoxPath stringByAppendingPathComponent:@"WeatherLike"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:collectionPath]) {
        [manager createDirectoryAtPath:collectionPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
}


+ (BOOL)ishaveCollected:(HZMOtherView *)list
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *collectionPath = [sandBoxPath stringByAppendingPathComponent:@"WeatherLike/weatherLike.plist"];
    NSArray *collectionArr = [NSKeyedUnarchiver unarchiveObjectWithFile:collectionPath];
    for (HZMOtherView *temp in collectionArr) {
        if ([temp.content isEqualToString:list.content]) {
            return NO;
        }
    }
    NSLog(@"创建文件成功");
    return YES;
}

+ (void)saveListToPlist:(HZMOtherView *)list
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    
    
    
    NSString *collectionPath = [sandBoxPath stringByAppendingPathComponent:@"WeatherLike/weatherLike.plist"];
    NSMutableArray *collectionArr = [NSKeyedUnarchiver unarchiveObjectWithFile:collectionPath];
    NSLog(@"表的地址: %@", collectionPath);
    if (collectionArr == nil) {
        collectionArr = [NSMutableArray array];
        
    }
    [collectionArr addObject:list];
    [NSKeyedArchiver archiveRootObject:collectionArr toFile:collectionPath];
    NSLog(@"存储成功");
}

+ (void)cancelListInPlist:(HZMOtherView *)list
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *collectionPath = [sandBoxPath stringByAppendingPathComponent:@"WeatherLike/weatherLike.plist"];
    NSMutableArray *collectionArr = [NSKeyedUnarchiver unarchiveObjectWithFile:collectionPath];
    for (HZMOtherView *temp in collectionArr) {
        if ([temp.content isEqualToString:list.content]) {
            [collectionArr removeObject:temp];
            break;
        }
    }
    [NSKeyedArchiver archiveRootObject:collectionArr toFile:collectionPath];
}



+ (NSMutableArray *)reload
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *collectionPath = [sandBoxPath stringByAppendingPathComponent:@"WeatherLike/weatherLike.plist"];
    NSMutableArray *collectionArr = [NSKeyedUnarchiver unarchiveObjectWithFile:collectionPath];
    return collectionArr;
}










@end











